from common.unit import AndroidUI
from common.home_page import HomePageObject
from common.qr_code import QRCodeX
from object.lnitial.lnitial_page import LnitialObject
from object.recover.recover_box_mode import RecoverBoxObject
import time


# 回收界面的重启蓝牙，置零，输入重量，科室和护士认证，选择不同类型的垃圾进行打印并装箱，对可回收和自定义垃圾打印并装箱，删除垃圾断言麻烦，暂不做
class RecoverFixBoxModeTest:
    def __init__(self):
        self.unit = AndroidUI
        self.home = HomePageObject()
        self.qrcode = QRCodeX()
        self.ln = LnitialObject()
        self.re = RecoverBoxObject()
        self.driver = AndroidUI.get_webdriver()

    def back_button(self):
        self.driver.keyevent(4)

    def scan_boxes(self, n):
        n2 = n + 1
        self.unit.scan(self.qrcode.box_id(n))  # 扫固定箱码n

    # 清理人登录
    # def check_login(self):
    #     # self.unit.scan(self.qrcode.zhaoqingli)
    #     try:
    #         self.unit().isElement("id","com.bluephone:id/systemSet4")
    #         self.unit.scan(self.qrcode.zhaoqingli)
    #         print("登录成功")
    #     except:
    #         print("已经登录")

    # 清理人登录
    def check_login(self):
        # self.unit.scan(self.qrcode.zhaoqingli)
        try:
            self.ln.system_button()
            self.unit.scan(self.qrcode.zhaoqingli)
            print("登录成功")
        except:
            print("已经登录")

    # 扫码进入回收界面，并且返回首页
    def enter_recover_scan(self):
        self.unit.scan(self.qrcode.waike)
        result = "认证成功"

        try:
            self.re.box_list_button()

            # result in self.re.department_button().text
            # if self.unit().isElement("text","认证成功"):
            # if self.unit().is_element_exist("认证成功"):
            print("扫码进入回收界面成功")
            AndroidUI().write_result("扫码进入回收界面", "成功")
            self.back_button()
        except:
            print("扫码进入回收界面失败")
            AndroidUI.capture()
            AndroidUI().write_result("扫码进入回收界面", "失败")
            # self.driver.quit()

    # 点击回收按钮，进入回收界面
    def enter_recover_click(self):
        try:
            self.home.recover_button().click()
            self.re.restart_bluetooth_button()
            print("点击回收按钮进入回收界面成功")
            AndroidUI().write_result("点击回收按钮进进入回收界面", "成功")
        except:
            print("点击回收按钮进进入回收界面失败")
            AndroidUI.capture()
            AndroidUI().write_result("点击回收按钮进进入回收界面", "失败")
            # self.driver.quit()

    # 重启蓝牙
    def restart_bluetooth(self):
        self.re.restart_bluetooth_button().click()
        print("点击重启蓝牙")
        AndroidUI().write_result("点击重启蓝牙", "成功")

    # 置零
    def clear_zero(self, weight):
        if weight == "不置零":
            print('不进行置零操作')
        else:
            self.re.clear_zero_button().click()  # 如果有连接秤就注销，后期优化为可选
            weight_text = self.re.weight_button().text
            # print(weight_text)
            if weight_text == "点击手输重量上传":
                # if "上传" in weight_text:
                AndroidUI().write_result("点击置零", "未连接蓝牙")
                print("点击置零按钮，提示未连接蓝牙")
                self.re.weight_button().click()
                self.re.weight_input().send_keys(weight)
                self.re.ok_button().click()
                print(f'设置重量为{weight}kg')
            elif "0.00" in self.re.weight_button().text:
                AndroidUI().write_result("点击置零", "置零成功")
                print("点击置零按钮，置零成功")
            else:
                AndroidUI().write_result("点击置零", "失败")
                AndroidUI.capture()
                print("点击置零按钮，置零失败")

    # 科室认证扫码，内科
    def department_scan(self):
        self.unit.scan(self.qrcode.neike)  # 扫内科的二维码
        if "内科" in self.re.department_button().text:
            print('扫码认证内科科室成功')
            AndroidUI().write_result("扫码认证内科", "成功")
        else:
            print('扫码认证内科科室失败')
            AndroidUI().write_result("扫码认证内科", "失败")
            AndroidUI.capture()

    # 手动选择科室
    def department_click(self, name):
        self.re.department_button().click()
        # self.driver.find_element_by_android_uiautomator('new UiSelector().textContains("外科")').click()
        # self.driver.find_element_by_android_uiautomator(f'new UiSelector().textContains("{name}")').click()
        try:
            self.driver.find_element_by_android_uiautomator(f'new UiSelector().textContains("{name}")').click()
            # print("选择成功。开始断言")
            if name in self.re.department_button().text:
                print(f'点击科室，选择认证{name}科室成功')
                AndroidUI().write_result(f"扫码认证{name}", "成功")
            else:
                print(f'选择认证{name}科室失败')
                AndroidUI().write_result(f"选择认证{name}", "失败")
                AndroidUI.capture()
        except:
            print(f'选择认证{name}科室失败')
            AndroidUI().write_result(f"选择认证{name}", "失败")
            AndroidUI.capture()

    # 扫码认证护士
    def nurse_scan(self):
        self.unit.scan(self.qrcode.qianhushi)  # 扫钱护士的二维码
        if "护士" in self.re.nurse_button().text:
            print('扫码认证护士成功')
            AndroidUI().write_result("扫码认证护士", "成功")
        else:
            print('扫码认证护士失败')
            AndroidUI().write_result("扫码认证护士", "失败")
            AndroidUI.capture()

    # 选择医废类型，通过selected属性判断是否选中
    def choose_type(self, name):
        # is_selected()返回的结果是True和False，is_displayed()：判断元素是否显示，is_selected()：判断元素是否选中状态
        # if self.driver.find_element_by_android_uiautomator(f'new UiSelector().textContains("{name}")').is_selected():
        #     print("选中")
        # else:
        #     print('没有选中')

        try:
            self.driver.find_element_by_android_uiautomator(f'new UiSelector().textContains("{name}")').click()
            if self.driver.find_element_by_android_uiautomator(
                    f'new UiSelector().textContains("{name}")').is_selected():
                print(f"选择医废类型{name}成功")
                AndroidUI().write_result(f"选择医废类型{name}", "成功")
            else:
                print(f"选择医废类型{name}失败")
                AndroidUI().write_result(f"选择类型{name}", "失败")
                AndroidUI.capture()
        except:
            print(f'选择医废类型{name}失败')
            AndroidUI().write_result(f"选择医废类型{name}", "失败")
            AndroidUI.capture()

    # 打印指定垃圾袋个两袋，装同一个箱子
    def print_two_bags(self, x, y):
        # weight_text = str(self.re.weight_button().text).strip('KG')     #设置的重量去掉KG的
        # print(weight_text)
        with open(r"D:\rj自动化\GUI_Appium\date\type_2bags.csv", encoding='utf-8') as file:
            content = file.readlines()
            for i, a in zip(content, range(x, y)):
                self.choose_type(i.strip())
                # m=i.strip()
                time.sleep(3)

                m = i.strip()
                new = f"，{m}"
                # new=f"{m}{weight_text}"
                # print(new)
                try:
                    self.print_bags()  # 打印操作
                    print('打印一袋垃圾')
                    time.sleep(4)
                    self.print_bags()  # 打印操作
                    self.driver.find_element_by_android_uiautomator(f'new UiSelector().textContains("NO.2")')
                    self.driver.find_element_by_android_uiautomator(f'new UiSelector().textContains("{new}")')
                    print(f"打印{m}垃圾两袋成功")
                    AndroidUI().write_result(f"打印医废类型为{m}的垃圾两袋", "成功")
                    # 打印成功后，开始装箱
                    try:
                        self.re.seal_box_button().click()  # 点击装箱按钮
                        self.scan_boxes(a)  # 扫固定箱码
                        if self.re.seal_box_button().is_enabled():
                            print(f'扫描固定箱码{a}以及通过enable判断装箱失败\n')
                            AndroidUI().write_result(f'扫描固定箱码{a}以及通过enable判断装箱', "失败")
                            AndroidUI.capture()
                        else:
                            print(f'扫描固定箱码{a}以及通过enable判断装箱成功\n')
                            AndroidUI().write_result(f'扫描固定箱码{a}以及通过enable判断装箱', "成功")
                    except:
                        print('点击装箱失败')
                        AndroidUI().write_result("点击装箱", "失败")
                        AndroidUI.capture()
                except:
                    print(f"打印{m}垃圾两袋失败")
                    AndroidUI().write_result(f"打印医废类型为{m}的垃圾两袋", "失败")
                    AndroidUI.capture()

    # 点击打印按钮
    def print_bags(self):
        self.re.print_button().click()

    # 点击装箱按钮
    def pack_box(self):
        self.re.seal_box_button().click()

    def pack_box_second(self, x, y, n):
        for i in range(x, y, n):
            i2 = i + 1
            self.scan_boxes(i)

    # 选择所有医废，并打印装箱
    def choose_all_type(self, x, y):
        # weight_text = str(self.re.weight_button().text).strip('KG')
        # print(weight_text)
        with open(r"D:\rj自动化\GUI_Appium\date\type.csv", encoding='utf-8') as file:
            content = file.readlines()
            '''          
            for i, a in zip(content, range(x, y))  
            zip函数， 函数用于将可迭代的对象作为参数，将对象中对应的元素打包成一个个元组，然后返回由这些元组组成的列表。
            如果各个迭代器的元素个数不一致，则返回列表长度与最短的对象相同，利用 * 号操作符，可以将元组解压为列表
            
            '''

            for i, a in zip(content, range(x, y)):
                # for n in range(x,y):
                self.choose_type(i.strip())
                # m=i.strip()
                m = i.strip()
                new = f"，{m}"
                time.sleep(3)
                # new=f"{m}{weight_text}"
                # print(new)
                try:
                    self.print_bags()  # 打印操作
                    self.driver.find_element_by_android_uiautomator(f'new UiSelector().textContains("{new}")')
                    print(f"打印{m}垃圾成功")
                    AndroidUI().write_result(f"打印医废类型为{m}的垃圾", "成功")
                    # 打印成功后，开始装箱
                    try:
                        self.re.seal_box_button().click()
                        self.scan_boxes(a)  # 扫固定箱码
                        if self.re.seal_box_button().is_enabled():
                            print(f"扫描固定箱码{a}以及通过enable判断装箱失败\n")
                            AndroidUI().write_result(f"扫描固定箱码{a}以及通过enable判断装箱", "失败")
                            AndroidUI.capture()
                        else:
                            print(f"扫描固定箱码{a}以及通过enable判断装箱成功\n")
                            AndroidUI().write_result(f"扫描固定箱码{a}以及通过enable判断装箱", "成功")
                    except:
                        print('点击装箱失败')
                        AndroidUI().write_result("点击装箱", "失败")
                        AndroidUI.capture()
                except:
                    print(f"打印{m}垃圾失败")
                    AndroidUI().write_result(f"打印医废类型为{m}的垃圾", "失败")
                    AndroidUI.capture()

                # #垃圾袋列表第一条数据
                #                 # first_list=self.driver.find_element_by_xpath("//android.widget.TextView[@resource-id='com.bluephone:id"
                #                 #                                              "/item_text' and @text='NO.2，损伤性5kg2020-10-08 08:39:25']")
                #                 # print(first_list)

    # NO.1，感染性6kg 2020-10-07 15:51:28
    '''
    从上往下
    //android.widget.ListView[@resource-id='com.bluephone:id/list_data']/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.ImageView[1]
    //android.widget.ListView[@resource-id='com.bluephone:id/list_data']/android.widget.LinearLayout[2]/android.widget.LinearLayout[1]/android.widget.ImageView[1]
    //android.widget.FrameLayout[@resource-id='android:id/content']/android.widget.LinearLayout[1]
    
    
    //android.widget.ListView[@resource-id='com.bluephone:id/list_data']/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.ImageView[1]
    //android.widget.ListView[@resource-id='com.bluephone:id/list_data']/android.widget.LinearLayout[2]/android.widget.LinearLayout[1]/android.widget.ImageView[1]
    //android.widget.ListView[@resource-id='com.bluephone:id/list_data']/android.widget.LinearLayout[3]/android.widget.LinearLayout[1]/android.widget.ImageView[1]
    '''

    # 对可回收垃圾进行打印斌装箱
    def recyclables(self, x, y):
        with open(r"D:\rj自动化\GUI_Appium\date\recycl_type.csv", encoding='utf-8') as file:
            content = file.readlines()

            for i, a in zip(content, range(x, y)):
                type = i.strip()
                new = f"，{type}"
                # print(type)
                try:
                    self.re.other_button().click()
                    self.driver.find_element_by_android_uiautomator(f'new UiSelector().text("{type}")').click()
                    print(f"选择类型{type}成功")
                    time.sleep(3)
                    self.print_bags()  # 打印
                    # 断言，打印并添加成功
                    try:
                        self.driver.find_element_by_android_uiautomator(f'new UiSelector().textContains("{new}")')
                        print(f"打印{type}垃圾成功")
                        AndroidUI().write_result(f"打印可回收垃圾{type}", "成功")
                        try:
                            self.re.seal_box_button().click()
                            self.scan_boxes(a)  # 扫固定箱码
                            if self.re.seal_box_button().is_enabled():
                                print(f'扫描固定箱码{a}以及通过enable判断装箱失败\n')
                                AndroidUI().write_result(f'扫描固定箱码{a}以及通过enable判断装箱', "失败")
                                AndroidUI.capture()
                            else:
                                print(f'扫描固定箱码{a}以及通过enable判断装箱成功\n')
                                AndroidUI().write_result(f'扫描固定箱码{a}以及通过enable判断装箱', "成功")
                        except:
                            print('点击装箱失败')
                            AndroidUI().write_result("点击装箱", "失败")
                            AndroidUI.capture()

                    except:
                        self.driver.find_element_by_android_uiautomator(f'new UiSelector().textContains("{new}")')
                        print(f"打印{type}垃圾失败")
                        AndroidUI.capture()
                        AndroidUI().write_result(f"打印可回收垃圾{type}", "失败")
                except:
                    print(f"点击其他，选择{type}失败")
                    AndroidUI.capture()
                    AndroidUI().write_result(f"点击其他，选择{type}", "失败")

    # 对自定义收垃圾进行打印装箱,类型在data的div_type设置
    def div_type(self, x, y):
        with open(r"D:\rj自动化\GUI_Appium\date\div_type.csv", encoding='utf-8') as file:
            content = file.readlines()

            for i, a in zip(content, range(x, y)):
                type = i.strip()
                new = f"，{type}"
                # print(type)
                try:
                    self.re.other_button().click()
                    self.re.type_input().send_keys(type)
                    self.re.other_ok_button().click()
                    print(f"选择自定义类型{type}成功")
                    time.sleep(3)
                    self.print_bags()  # 打印
                    # 断言，打印并添加成功
                    try:
                        self.driver.find_element_by_android_uiautomator(
                            f'new UiSelector().textContains("{new}")')
                        print(f"打印{type}垃圾成功")
                        AndroidUI().write_result(f"打印可回收垃圾{type}", "成功")
                        try:
                            self.re.seal_box_button().click()
                            self.scan_boxes(a)  # 扫固定箱码
                            if self.re.seal_box_button().is_enabled():
                                print(f'扫描固定箱码{a}以及通过enable判断装箱失败\n')
                                AndroidUI().write_result(f'扫描固定箱码{a}以及通过enable判断装箱', "失败")
                                AndroidUI.capture()
                            else:
                                print(f'扫描固定箱码{a}以及通过enable判断装箱成功\n')
                                AndroidUI().write_result(f'扫描固定箱码{a}以及通过enable判断装箱', "成功")
                        except:
                            print('点击装箱失败')
                            AndroidUI().write_result("点击装箱", "失败")
                            AndroidUI.capture()
                    except:
                        self.driver.find_element_by_android_uiautomator(
                            f'new UiSelector().textContains("{new}")')
                        print(f"打印{type}垃圾失败")
                        AndroidUI.capture()
                        AndroidUI().write_result(f"打印可回收垃圾{type}", "失败")
                except:
                    print(f"点击其他，选择{type}失败")
                    print(f"打印{type}垃圾失败")
                    AndroidUI.capture()
                    AndroidUI().write_result(f"点击其他，选择并打印{type}", "失败")

    # 返回首页
    def back_home(self):
        try:
            self.re.back_button().click()
            self.home.home_page()
            print("返回首页成功")
            AndroidUI().write_result("返回首页", "成功")
        except:
            print("返回首页失败")
            self.back_button()
            AndroidUI.capture()
            AndroidUI().write_result("返回首页界面", "失败")



    # 括号里的取值需要连贯，否则会出问题
    def start_test(self, weight, keshi):
        self.check_login()
        self.enter_recover_scan()
        self.enter_recover_click()
        self.clear_zero(weight)  # 如果未连接情况下，则输入0.5kg
        self.department_scan()
        self.department_click(keshi)
        self.nurse_scan()
        self.choose_all_type(1, 7)  # 打印所有类型,并装箱，1到6
        self.recyclables(7, 10)  # 打印可回收三类型,并装箱         7到9
        self.div_type(10, 13)  # 打印自定义垃圾,并装箱      10到12
        self.print_two_bags(13, 16)
        self.back_home()


if __name__ == '__main__':
    re = RecoverFixBoxModeTest()
    # re.check_login()
    re.start_test('0.2', "自动化")
    # re.choose_type('损伤性')
